package com.sanpu.kxe.business.member.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.member.adapter.MemberProductAdapter;
import com.sanpu.kxe.business.member.bean.MemberProductRateOutput;
import com.sanpu.kxe.business.member.contract.MemberProductRateContract;
import com.sanpu.kxe.business.member.presenter.MemberProductRatePresenter;
import com.sanpu.kxe.business.member.provider.MemberOpenProvider;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.widget.CenterScrollListener;
import com.sanpu.kxe.module.widget.OverFlyingLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Description：会员产品费率
 * Created by 薛勇军 on 2019/4/15.
 */

public class MemberProductRateActivity extends BaseTitleActivity implements MemberProductRateContract.View {

    @Bind(R.id.recyclerView)
    RecyclerView vRecyclerView;
    @Bind(R.id.repayCnlLl)
    LinearLayout repayCnlLl;
    @Bind(R.id.payCnlLl)
    LinearLayout payCnlLl;
    @Bind(R.id.upgradeRtv)
    RoundTextView upgradeRtv;
    @Bind(R.id.groupLl)
    LinearLayout groupLl;
    @Bind(R.id.levelNameTv)
    TextView levelNameTv;
    @Bind(R.id.openConditionsLl)
    LinearLayout openConditionsLl;
    @Bind(R.id.openConditionsTv)
    TextView openConditionsTv;

    private OverFlyingLayoutManager mOverFlyingLayoutManager;

    private MemberProductAdapter mProductAdapter;

    private MemberProductRateContract.Presenter mPresenter;

    private List<MemberProductRateOutput> mRateOutputs = new ArrayList<>();

    private int mCurrentLevelId;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_member_rate;
    }

    @Override
    public void initView() {
        super.initView();
        commonTitle.setTitle("升级会员");
        UserCenterOutput userCenterOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        mProductAdapter = new MemberProductAdapter(mContext, userCenterOutput);
        vRecyclerView.addOnScrollListener(new CenterScrollListener());
        vRecyclerView.setAdapter(mProductAdapter);
        mOverFlyingLayoutManager = new OverFlyingLayoutManager(1f, 0, 0, OverFlyingLayoutManager.HORIZONTAL);
        vRecyclerView.setLayoutManager(mOverFlyingLayoutManager);

        mPresenter = new MemberProductRatePresenter(this);
        mPresenter.productRateList();
    }

    @Override
    protected void initData() {
        super.initData();
        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null) {
            mCurrentLevelId = centerOutput.getLevelId();
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        mOverFlyingLayoutManager.setOnPageChangeListener(new OverFlyingLayoutManager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                showCnlListView(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        upgradeRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (!mPresenter.isAuthenPass()) return;
                MemberProductRateOutput rateOutput = (MemberProductRateOutput) v.getTag();
                if (rateOutput != null) {
                    MemberOpenProvider.getInstance().openMemberCashierActivity(mContext,
                            rateOutput.getAmount(), rateOutput.getLevelName(), rateOutput.getVipOpenId());
                }
            }
        });
    }

    @Override
    public void showDataView(List<MemberProductRateOutput> rateOutputs) {
        if (rateOutputs != null && !rateOutputs.isEmpty()) {
            mRateOutputs.clear();
            mRateOutputs.addAll(rateOutputs);
            int chooseIndex = 0;
            for (int index = 0; index < rateOutputs.size(); index++) {
                if (mCurrentLevelId == rateOutputs.get(index).getLevelId()) {
                    chooseIndex = index;
                    break;
                }
            }
            groupLl.setVisibility(View.VISIBLE);
            mProductAdapter.setNotifyData(mRateOutputs);
            mOverFlyingLayoutManager.scrollToPosition(chooseIndex);
            showCnlListView(chooseIndex);
        } else {
            groupLl.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 显示通道列表
     */
    private void showCnlListView(int position) {
        if (mRateOutputs.size() > position) {
            MemberProductRateOutput rateOutput = mRateOutputs.get(position);
            List<MemberProductRateOutput.CnlList> cnlLists = rateOutput.getRepayCnlList();
            if (cnlLists == null) {
                cnlLists = new ArrayList<>();
            }
            repayCnlLl.removeAllViews();
            for (int i = 0; i < cnlLists.size(); i++) {
                repayCnlLl.addView(createMenuItem(repayCnlLl, cnlLists.get(i)));
            }

            List<MemberProductRateOutput.CnlList> payCnlLists = rateOutput.getPayCnlList();
            if (payCnlLists == null) {
                payCnlLists = new ArrayList<>();
            }
            payCnlLl.removeAllViews();
            for (int i = 0; i < payCnlLists.size(); i++) {
                payCnlLl.addView(createMenuItem(payCnlLl, payCnlLists.get(i)));
            }
            if (!TextUtils.isEmpty(rateOutput.getLevelName())) {
                levelNameTv.setText("" + rateOutput.getLevelName() + "特权");
            }
            if (!TextUtils.isEmpty(rateOutput.getOpenConditions())) {
                openConditionsTv.setText(rateOutput.getOpenConditions());
                openConditionsLl.setVisibility(View.VISIBLE);
            } else {
                openConditionsLl.setVisibility(View.GONE);
            }
        }
    }

    /**
     * Create Menu Item View
     * @return View
     */
    private View createMenuItem(ViewGroup viewGroup, MemberProductRateOutput.CnlList cnlList) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_member_product_rate, viewGroup, false);

        TextView cnlNameTv = ButterKnife.findById(itemView, R.id.cnlNameTv);
        TextView repayRateTv = ButterKnife.findById(itemView, R.id.repayRateTv);
        TextView singlimitTv = ButterKnife.findById(itemView, R.id.singlimitTv);
        //用来显示按钮名称
        if (cnlList != null) {
            if (!TextUtils.isEmpty(cnlList.getCnlName())) {
                cnlNameTv.setText(cnlList.getCnlName());
            }
            if (!TextUtils.isEmpty(cnlList.getRepayRate())) {
                repayRateTv.setText(cnlList.getRepayRate());
            }
            if (!TextUtils.isEmpty(cnlList.getSinglimit())) {
                singlimitTv.setText(cnlList.getSinglimit());
            }
        }
        return itemView;
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MemberProductRateContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
